import 'package:audioplayerssample/utils/audiodata.dart';
import 'package:flutter/material.dart';
import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'dart:ui' as ui show window;
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: '语音',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title:"浩哥的语音包混合"),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);


  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {



  void stopall() {
    setState(() {

      for(int i=0;i<musiclist.length;i++)
        {
        musiclist[i][ "audioplayers"].stop();
        musiclist[i]["play"]=true;
        }

    });
  }
  AudioPlayer ap;
  void  playorstop(v) async
  {

    AudioCache s=new AudioCache();
    if(v["title"]=="鸡你太美")
      {
        v["play"]?  v["audioplayers"]= await s.play(v["audiourl"]):v["audioplayers"].stop();
        v["play"]=!v["play"];

      }
    else
      {
        v["audioplayers"]= await AudioCache().play(v["audiourl"]);
      }

    return;
  }
  @override
  Widget build(BuildContext context) {


    ScreenUtil.init(context,width: 360,height: 780);
    return SafeArea(

      child:
      Scaffold(
        appBar: PreferredSize(

          preferredSize: Size(double.infinity, ScreenUtil.screenHeightDp*0.06),
          child:AppBar(
            // Here we take the value from the MyHomePage object that was created by
            // the App.build method, and use it to set our appbar title.
            title: Text(widget.title),
            centerTitle: true,
          ) ,
        ) ,
        body: Column(

//  mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[

            Container(

              height: ScreenUtil.screenHeightDp/2,
              width: ScreenUtil.screenWidthDp,
              child: Container(

                child: Column(



                  children: <Widget>[

                    Padding(

                      padding: EdgeInsets.fromLTRB(0,ScreenUtil().setHeight(10),0,0),
                      child:
                      Container(

                        height:ScreenUtil.screenHeightDp/2-ScreenUtil().setHeight(10),
                        width: ScreenUtil.screenWidthDp.toDouble(),
                        decoration: BoxDecoration(

                          image: new DecorationImage(
                            image: new AssetImage("assets/images/jinitaimei.gif"),
                            //这里是从assets静态文件中获取的，也可以new NetworkImage(）从网络上获取
                            centerSlice: new Rect.fromLTRB(270.0, 180.0, 1360.0, 730.0),
                          ),

                        ),
                      ),
                    ),




                  ],
                ),
              ),

            ) ,


            Container(

              height: ScreenUtil.screenHeightDp/2-ScreenUtil.screenHeightDp*0.06- MediaQueryData.fromWindow(ui.window).padding.top,
              width: ScreenUtil.screenWidthDp,
              child:  GridView.builder(
                  itemCount: musiclist.length,
                  //SliverGridDelegateWithFixedCrossAxisCount 构建一个横轴固定数量Widget
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    
                      crossAxisCount: 4,
                      
                      mainAxisSpacing: 10.0,
                      
                      crossAxisSpacing: 5.0,
                     
                      childAspectRatio: 2.0),
                  itemBuilder: (BuildContext context, int index) {
                    //Widget Function(BuildContext context, int index)
                    return Container(
                      child:  new RaisedButton(
                        child: new Text(musiclist[index]["title"],style: TextStyle(fontSize: ScreenUtil().setSp(11)),),
                        onPressed: () {
                          playorstop(musiclist[index]);

                        },
                      ),);
                  }),
            ),
          ],
        ),





        floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
        floatingActionButton: FloatingActionButton(
          onPressed: stopall,
          tooltip: '关闭所有音频',
          child: Icon(Icons.stop),
        ), // This trailing comma makes auto-formatting nicer for build methods.
      ),
    );
  }
}
